package cn.jsj.onclock.controller;

import cn.jsj.onclock.interceptor.RequestThreadContext;
import cn.jsj.onclock.interceptor.swich.PassToken;
import cn.jsj.onclock.pojo.LabelEnt;
import cn.jsj.onclock.pojo.identity.UserIdentityEnum;
import cn.jsj.onclock.result.Result;
import cn.jsj.onclock.service.LabelService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 卢斌
 */
@Controller
@CrossOrigin
@ResponseBody
@RequestMapping(value = "/label")
public class LabelController {

    private final LabelService labelService;

    public LabelController(LabelService labelService) {
        this.labelService = labelService;
    }


    /**
     * Id获取标签实体 （不需要验证用户身份）
     */
    @PassToken
    @GetMapping(value = "/label", params = {"labelId"})
    public Result<LabelEnt> oneLabel(@RequestParam("labelId") Long labelId) {
        LabelEnt labelEnt = labelService.getOneLabelById(labelId);
        return Result.success(labelEnt);
    }


    /**
     * 管理员录入标签卡信息
     */
    @PostMapping(value = "/label")
    public Result<Object> insertOne(@RequestBody LabelEnt labelEnt) {
        UserIdentityEnum identity = RequestThreadContext.requesterIdentity();
        labelService.insertOneLabel(identity, labelEnt.getTitle(), labelEnt.getLabelNum());
        return Result.success();
    }


    /**
     * 管理员更新标签名称
     */
    @PutMapping(value = "/label")
    public Result<Object> updateTitle(@RequestBody LabelEnt labelEnt) {
        UserIdentityEnum identity = RequestThreadContext.requesterIdentity();
        labelService.updateLabelTitle(labelEnt.getId(), identity, labelEnt.getTitle());
        return Result.success();
    }


    /**
     * 查询符合名称前缀的标签列表
     */
    @PassToken
    @GetMapping(value = "/like_title", params = {"prefix", "pageNum", "pageSize"})
    public Result<LabelEnt> likeTitle(@RequestParam("prefix") String prefix, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        List<LabelEnt> storage = labelService.listLabelLikeTitlePrefix(prefix, pageNum, pageSize);
        return Result.success(storage);
    }


    /**
     * 模糊查询标签
     */
    @PassToken
    @GetMapping(value = "/soul_select", params = {"pareTitle"})
    public Result<LabelEnt> likeTitle(@RequestParam("pareTitle") String pareTitle) {
        List<LabelEnt> storage = labelService.listLabelLikeTitle(pareTitle);
        return Result.success(storage);
    }


    /**
     * 查询全部的签到标签
     */
    @PassToken
    @GetMapping(value = "/all_label", params = {"pageNum", "pageSize"})
    public Result<LabelEnt> allLabel(Integer pageNum, Integer pageSize) {
        List<LabelEnt> storage = labelService.listAllLabel(pageNum, pageSize);
        return Result.success(storage);
    }


}
